package com.intellagric.controller.module.sys_office;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.SysOffice;
import com.intellagric.service.module.sys_office.OfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/12
 * @DESC： 组织机构管理
 */
@Controller
public class OfficeController {
    
    @Autowired
    private OfficeService officeService;

    /**
     * 添加组织机构信息
     * @Param SysOffice
     * @return ResponseMessage
     */
    @RequestMapping("/office/add")
    @ResponseBody
    public ResponseMessage add(SysOffice sysOffice) {
        //设置默认数据
        sysOffice.setId(UUID.randomUUID().toString().replaceAll("-",""));
        sysOffice.setCreateDate(new Date());
        sysOffice.setUpdateDate(new Date());
        return officeService.addOffice(sysOffice);
    }


    /**
     * 删除组织机构信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("/office/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return officeService.deleteOffice(id);
    }

    /**
     * 修改组织机构信息
     * @Param SysOffice
     * @return ResponseMessage
     */
    @RequestMapping("/office/edit")
    @ResponseBody
    public ResponseMessage edit(SysOffice sysOffice) {
        //设置默认信息
        sysOffice.setUpdateDate(new Date());
        return officeService.editOffice(sysOffice);
    }

    /**
     * 查询组织机构信息
     * @return SysOffice
     */
    @RequestMapping("/office/{id}")
    @ResponseBody
    public SysOffice get(@PathVariable String id) {
        return officeService.getOfficeById(id);
    }

    /**
     * 异步查询组织机构信息列表
     * @return SysOffice
     */
    @RequestMapping("/office/{parentId}/sublist")
    @ResponseBody
    public LayuiDataGridResult getSubList(@PathVariable String parentId) {
        return officeService.getOfficeByParentId(parentId);
    }

    /**
     * 查询组织机构树信息，只返回树结点的数据
     * @return LayuiDataGridResult
     */
    @RequestMapping("/office/treelist")
    @ResponseBody
    public List<SysOffice> getTreeList() {
        LayuiDataGridResult office = officeService.getOfficeTree();
        return office.getData();
    }

    /**
     * 查询组织机构树信息
     * @return LayuiDataGridResult
     */
    @RequestMapping("/office/tree")
    @ResponseBody
    public LayuiDataGridResult getTree() {
        return officeService.getOfficeTree();
    }

    /**
     * 查询组织机构信息
     * @return LayuiDataGridResult
     */
    @RequestMapping("/office/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return officeService.getOfficeList();
    }

}
